import java.util.ArrayList;
import java.util.List;

public class LetterCombinationsofaPhoneNumber0102_17 {

    List<String> result = new ArrayList<>();
    StringBuilder s = new StringBuilder();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return result;
        }

        String[] stringMap = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        backtracking(digits,stringMap, 0);
        return result;
    }

    public void backtracking(String digits, String[] stringMap, int index){

        if(index == digits.length()) {
            result.add(s.toString());
            return;
        }

        String str = stringMap[digits.charAt(index) - '0'];

        for (int i = 0; i < str.length(); i++) {
            s.append(str.charAt(i));
            backtracking(digits,stringMap,index+1);
            s.deleteCharAt(s.length() - 1);
        }
    }


}
